#!/usr/bin/env node

// const { program } = require("commander");
// const version = require("../package.json").version;
// program.version(version, "-v, --version");
// console.log("hello haocli");
// console.log(program.parse(process.argv));

// program
//   .command("init")
//   .description("初始化项目")
//   .action(function () {
//     console.log("init");
//   });

// 获取运行命令时传来的参数
// const { program } = require("commander");
// program.option("-d --dest <dest>", "获取传来的参数，如传来的是文件夹路径");
// program.parse(process.argv); // 此行要有
// const p = program.opts();
// console.log(p, "-d 传来的参数");

// 创建项目
const { program } = require("commander");
const { promisify } = require("util"); // 此为node内置的包，无需安装
const download = promisify(require("download-git-repo")); // download-git-repo的download不能异步，故使用promisify将其修饰一下

const { spawn } = require("child_process");
const gitPath = "direct:https://gitee.com/wuhun1990/vite-project.git#mast"; //direct代表侦测  #mast代表mast分支 默认会下载mast分支

// 封装命令
const execCommand = (command, data, project) => {
  return new Promise((resolve) => {
    // 开启子进程执行命令
    const childProcess = spawn(command, data, project);
    // 获取子进程的输出和错误信息
    childProcess.stdout.pipe(process.stdout);
    childProcess.stderr.pipe(process.stderr);
    childProcess.on("close", () => {
      resolve();
    });
  });
};
// 命令 参数 在哪个文件夹下执行此命令
// const execCommand = (command) => {
//   return new Promise((resolve, reject) => {
//     const cp = spawn(command, [], {
//       stdio: "inherit",
//     });
//     cp.on("close", (code) => {
//       if (code !== 0) {
//         reject({
//           command: command,
//           code: code,
//         });
//       }
//       resolve(command);
//     }
//     );
//   }
//   );
// };

// 封装clone方法
const createProjectAction = async (projectName) => {
  try {
    // 拉取项目
    await download(gitPath, projectName, { clone: true });
    console.log("创建成功");
    // 适配 window平台为npm.cmd
    const commandName = process.platform === "win32" ? "npm.cmd" : "npm";
    // install项目
    console.log("node_modules安装中~");
    await execCommand(commandName, ["install"], { cwd: `./${projectName}` });
    console.log("安装成功");
    // run 项目
    console.log("项目启动中~");
    await execCommand(commandName, ["run", "dev"], { cwd: `./${projectName}` });
    console.log("启动成功");
  } catch (error) {
    console.log("项目创建失败：" + error);
    // // 适配 window平台为npm.cmd
    // const commandName = process.platform === "win32" ? "npm.cmd" : "npm";
    // // install项目
    // console.log("node_modules安装中~");
    // await execCommand(commandName, ["install"], { cwd: `./${projectName}` });
    // console.log("安装成功");
    // // run 项目
    // console.log("项目启动中~");
    // await execCommand(commandName, ["run", "dev"], { cwd: `./${projectName}` });
    // console.log("启动成功");
  }
};
// 监听输入的命令
program
  .command("create <project-name>")
  .description("创建项目")
  .action(function (projectName) {
    // 从github上将编写的模板下载下来
    console.log(projectName);
    createProjectAction(projectName);
  });
program.parse(process.argv);
